package core;

import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import utils.DBG;
import utils.Properties;
import core.rendering.RenderableMouse;

public class Camera extends RenderableMouse implements Runnable {

	private static Camera instance;

	float xpos;
	float ypos;
	int width;
	int height;
	Rectangle bounds;
	int[] velocity = { 3, 9, 16 };

	private Camera() {
		xpos = 0;
		ypos = 0;
		DBG.showMsg("Camara Iniciada >>>>>>>>>>>>>>>", this);
		Thread t = new Thread(this);
		t.start();
	}

	public static Camera getInstance() {
		if (instance == null) {
			instance = new Camera();
		}
		return instance;
	}

	public void run() {
		while (true) {
			try {
				PointerInfo p = MouseInfo.getPointerInfo();
				Point poi = p.getLocation();
				updateMouse(poi);
				DBG.showMsg("c.", this);
				Thread.sleep(20);
			} catch (Exception e) {
				DBG.showMsg("erorr ne loop de camara", this);
			}

		}
	}

	public void setBounds(Rectangle r) {
		this.bounds = r;
		this.move((float) bounds.getLocation().x,
				(float) bounds.getLocation().y);
	}

	public void move(float x, float y) {
		xpos = x;
		ypos = y;
	}

	public int getIntX() {
		return (int) xpos;
	}

	public int getIntY() {
		return (int) ypos;
	}

	public void moveIncrementX(int amount) {
		xpos += amount;

		if (xpos > bounds.getLocation().x) {
			xpos = (float) bounds.getLocation().x;
		}

		System.out.println("XPS : " + xpos + " [ " + bounds.getWidth());
		if ((xpos - Properties.SCREEN_WIDTH + bounds.getWidth()) < 0) {
			xpos = -(float) bounds.getWidth() + Properties.SCREEN_WIDTH;
		}

	}

	public void moveIncrementY(int amount) {
		ypos += amount;

		if (ypos > bounds.getLocation().y) {
			ypos = (float) bounds.getLocation().y;
		}

		System.out.println("yPS : " + ypos + " [ " + bounds.getHeight());
		if ((ypos - Properties.SCREEN_HEIGHT + bounds.getHeight()) < 0) {
			ypos = -(float) bounds.getHeight() + Properties.SCREEN_HEIGHT;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see core.rendering.RenderableMouse#onClick(java.awt.event.MouseEvent)
	 */
	@Override
	public boolean onClick(MouseEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see core.rendering.RenderableMouse#updateMouse(java.awt.Point)
	 */
	@Override
	public void updateMouse(Point p) {
		// TODO Auto-generated method stub
		p.x -= GameFlow.getInstance().getLocationOnScreen().x;
		p.y -= GameFlow.getInstance().getLocationOnScreen().y;
		if (p.x < 30) {
			this.moveIncrementX(velocity[2]);
		} else {
			if (p.x < 60) {
				this.moveIncrementX(velocity[1]);
			} else if (p.x < 120) {
				this.moveIncrementX(velocity[0]);
			}
		}

		if (p.x > Properties.SCREEN_WIDTH - 30) {
			this.moveIncrementX(-velocity[2]);
		} else {
			if (p.x > Properties.SCREEN_WIDTH - 60) {
				this.moveIncrementX(-velocity[1]);
			} else if (p.x > Properties.SCREEN_WIDTH - 100) {
				this.moveIncrementX(-velocity[0]);
			}
		}

		if (p.y < 30) {
			this.moveIncrementY(velocity[2]);
		}else{
			if (p.y < 60) {
				this.moveIncrementY(velocity[1]);
			}else if (p.y < 100){
				this.moveIncrementY(velocity[0]);
			}
		}
		
		if (p.y > Properties.SCREEN_HEIGHT - 30) {
			this.moveIncrementY(-velocity[2]);
		}else{
			if (p.y > Properties.SCREEN_HEIGHT - 60) {
				this.moveIncrementY(-velocity[1]);
			}else if (p.y > Properties.SCREEN_HEIGHT - 100) {
				this.moveIncrementY(-velocity[0]);
			}	
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see core.rendering.Renderable#render(java.awt.Graphics2D)
	 */
	@Override
	public void renderComponent(Graphics2D g2d) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see core.rendering.Renderable#updateLogic()
	 */
	@Override
	public void updateLogic() {
		// TODO Auto-generated method stub

	}

}
